{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0bd32972-13e8-475c-9415-c50a50409568",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e35e35f2-89e4-4243-8c6c-bf3abe8b422c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!cd modelscope-agent && pip install -r requirements.txt\n",
    "! pip install transformers_stream_generator\n",
    "! pip install invisible-watermark>=2.0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c1ede08-857f-4592-9093-d7e5a37ce245",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/demo')\n",
    "\n",
    "\n",
    "import sys\n",
    "sys.path.append('../')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ad42f062-5664-44f8-82b7-6995a4f512a0",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 读取环境变量文件和工具config配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f06ad1-34c5-4032-9254-aab88d7fe19d",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from modelscope.utils.config import Config\n",
    "\n",
    "# 配置环境变量，里面需要配置modelscope token来remote调用AI模型\n",
    "os.environ['TOOL_CONFIG_FILE'] ='../config/cfg_tool_template.json'\n",
    "os.environ['OUTPUT_FILE_DIRECTORY'] = '/tmp'\n",
    "\n",
    "# ModelScope Token从这里获取: https://modelscope.cn/my/myaccesstoken\n",
    "print('请输入ModelScope Token，可以从这里获取: https://modelscope.cn/my/myaccesstoken')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()\n",
    "\n",
    "# 读取工具调用CONFIG文件，包括工具的名称和远程请求url\n",
    "tool_cfg_file = os.getenv('TOOL_CONFIG_FILE')\n",
    "print(tool_cfg_file)\n",
    "tool_cfg = Config.from_file(tool_cfg_file)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "aadebd3f-e761-4910-bffe-2a6130f50e0a",
   "metadata": {},
   "source": [
    "### 构建工具调用向量检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "774ab665-d07f-49fb-8826-2d865393fae9",
   "metadata": {
    "ExecutionIndicator": {
     "show": false
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.embeddings import ModelScopeEmbeddings, DashScopeEmbeddings\n",
    "from langchain.vectorstores import FAISS\n",
    "from modelscope_agent.retrieve import ToolRetrieval\n",
    "\n",
    "# 第一种向量方案，基于阿里云DashScope向量API服务方式，需要开通DashScope文本向量服务()\n",
    "# embeddings = DashScopeEmbeddings(model=\"text-embedding-v1\")\n",
    "\n",
    "# 第二种向量方案，基于魔搭社区开源向量模型\n",
    "model_id = 'damo/nlp_corom_sentence-embedding_chinese-base'\n",
    "embeddings = ModelScopeEmbeddings(model_id=model_id)\n",
    "\n",
    "# 基于FAISS构建向量检索引擎\n",
    "tool_retrieval = ToolRetrieval(embeddings, FAISS) \n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "325572f8-7960-479b-a91d-052a417b66ae",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 中枢大模型启动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed061825-7e6a-4edd-bc27-ffb97b168681",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from modelscope_agent.llm import LLMFactory\n",
    "from modelscope.hub.api import HubApi\n",
    "\n",
    "model_name = 'modelscope-agent-7b'\n",
    "model_cfg = {\n",
    "    'modelscope-agent-7b':{\n",
    "        'type': 'modelscope',\n",
    "        'model_id': 'damo/ModelScope-Agent-7B',\n",
    "        'model_revision': 'v1.0.0',\n",
    "        'use_raw_generation_config': True,\n",
    "        'custom_chat': True\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "llm = LLMFactory.build_llm(model_name, model_cfg)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "32d7bd5f-2df4-436c-b92c-75b625479379",
   "metadata": {},
   "source": [
    "### Agent构建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f65059d-9a54-42b8-bb26-7f320e3bfbc0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from modelscope_agent.prompt import MSPromptGenerator\n",
    "from modelscope_agent.agent import AgentExecutor\n",
    "\n",
    "prompt_generator = MSPromptGenerator(system_template=\"你是达摩院的ModelScopeGPT（魔搭助手），你是个大语言模型， 是2023年达摩院的工程师训练得到的。\\\n",
    "你有多种能力，可以通过插件集成魔搭社区的模型api来回复用户的问题，还能解答用户使用模型遇到的问题和模型知识相关问答。\")\n",
    "\n",
    "# 构建Agent，需要传入llm，工具配置config以及工具检索\n",
    "agent = AgentExecutor(llm, tool_cfg, tool_retrieval=tool_retrieval, prompt_generator=prompt_generator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7eed2650-f9ba-4d09-86d1-58e24ab27b38",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 重置对话，清空对话历史\n",
    "agent.reset()\n",
    "\n",
    "# remote=True为调用modelscope api，该服务免费支持QPS较低，建议部署在本地，将remote=False\n",
    "agent.run('使用地址识别模型，从下面的地址中找到省市区等元素，地址：浙江杭州市江干区九堡镇三村村一区', remote=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0579731",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Multi-turn tool-use\n",
    "agent.reset()\n",
    "agent.run('写一个20字左右简短的小故事')\n",
    "agent.run('用女声念出来', remote=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
